
#include "stdafx.h"
#include <windows.h>
#include <commctrl.h>
#include <profiler.h>


#define DEF_SMPIO_CONSOLE		// use debugout as the default io
#define KEN_LIB_IMP
#include "SmpIO.h"
#include "stubs.h"
#include "CmdLnPars.h"

const static TCHAR* s_pszUsage=
_T("usage:\r\n")\
_T("\tIO options:\r\n")\
_T("\t\tin_type:[console(default), file;filename] /input_file:\r\n")\
_T("\t\tout_type:[console(default), debugout, file]  /output_file:filename\r\n")\
_T("\t\t/interact\r\n")\
_T("\tprofile options:\r\n")\
_T("\t\t/do:[start, pause, resume, startpause, stop]\r\n")\
_T("\t\t/data_type:[monte, syscall, kcall] [/stack:[all, inproc]]\r\n")\
_T("\t\t/store:[buf, celog, unbuf]\r\n")\
_T("\t\t/interval:ms\r\n")\
_T("\t\t/celog_flush:file_name /celog_buf:0xXXXX /celog_clear\r\n")\
;
 
VOID Usage()
{
	g_SmpIO.Puts(s_pszUsage);
}


#if (defined(WIN32) && defined(_CONSOLE))
int _tmain(int argc, const _TCHAR* argv[])
{
	CCmdLineParser cmdLnParser;

	// parse command options
	cmdLnParser.ParseArg(argc, argv);
#else
int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
	CCmdLineParser cmdLnParser;

	// parse command options
	cmdLnParser.ParseCmdLine(lpCmdLine);
#endif


	if( (0==cmdLnParser.GetOptionCnt())
		|| cmdLnParser.GetOption(_T("h"))
		|| cmdLnParser.GetOption(_T("help"))
		|| cmdLnParser.GetOption(_T("usage")) )
	{
		Usage();
		return 0;
	}


	_tstring str;
	cmdLnParser.GetOption(_T("do"), &str);
	if(str==_T("stop"))
	{
		ProfileStop();
	}
	else
	{
		ProfilerControl pc={0};
		pc.dwVersion=1;
		pc.dwOptions=PROFILE_CELOG|PROFILE_START/*|PROFILE_CALLSTACK|PROFILE_CALLSTACK_INPROC*/;
		pc.Kernel.dwUSecInterval=200;
		ProfileStartEx(&pc);
	}

	return 0;
}